﻿/*
 * @author: wizardc
 */

using System.IO;
using Dou.Resource;
using DouEditor.Const;
using DouEditor.Utils;

namespace DouEditor.Resource.HotFixed
{
    /// <summary>
    /// 收集热更新数据清单文件
    /// </summary>
    public class CollectHotFixedManifest
    {
        public void Collect(BuildRule buildRule)
        {
            var hotFixedManifest = new HotFixedManifest();

            var files = Directory.GetFiles(buildRule.copyPath, "*.*", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                var ext = Path.GetExtension(file);
                if (ext == BuildConst.ManifestExtension)
                {
                    continue;
                }
                var name = EditorUtil.ConvertToRelativePath(buildRule.copyPath, file);
                var fileInfo = new FileInfo(file);
                hotFixedManifest.fileInfoMap.Add(name, new HotFixedManifest.FileInfo
                {
                    fileName = name,
                    hashCode = EditorUtil.GetFileMD5Code(file),
                    size = (int) fileInfo.Length
                });
            }
            
            var content = hotFixedManifest.Encode();
            EditorUtil.SaveTxtFile(Path.Combine(buildRule.copyPath, BuildConst.HotFixedManifestName), content);
        }
    }
}
